/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import models.User;
import org.hibernate.Session;
import service.HibernateUtil;
import service.UserDao;

/**
 *
 * @author Aster
 */
public class AdminController extends HttpServlet {

    private HibernateUtil HU = new HibernateUtil();
    private Session sessie = HU.openSession();
    private UserDao ud = new UserDao(sessie);
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        saveUsers(request, response);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException{
         saveUsers(request, response);
        
    }
    
    public void saveUsers(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException{
        boolean admin = false;
        String firstName = new String();
        String lastName = new String();
        for (Cookie cookie : request.getCookies()) {
            if (cookie.getName().equals("status") && cookie.getValue().equals("Admin")) {
                admin = true;
            }
            if (cookie.getName().equals("firstname")){
                firstName = cookie.getValue();
            } else if (cookie.getName().equals("lastname")){
                lastName = cookie.getValue();
            }
        }
        if (admin == true) {
            //response.sendRedirect("admin.jsp");
            HttpSession sessie = request.getSession(true);
            List<User> users = ud.findAll();
            List<User> students = new ArrayList<User>();
            List<User> teachers = new ArrayList<User>();
            for (User currentUser : users) {
                if ("Student".equals(currentUser.getStatus())||"Disabled".equals(currentUser.getStatus())) {
                    students.add(currentUser);
                }
                if ("Tutor".equals(currentUser.getStatus())||"Admin".equals(currentUser.getStatus())) {
                    teachers.add(currentUser);
                }
               // System.out.println(currentUser.getStatus());
            }
            sessie.setAttribute("students", students);
            sessie.setAttribute("teachers", teachers);
            sessie.setAttribute("name", firstName + " " + lastName);
            forward(request, response);

        } else {
            response.sendRedirect("/");
        }
    }
    
    private void forward(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //request.setAttribute("pageTitle", titel);

        String address = "/admin.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }
}
